# coding:utf-8

import os
import logging

import django.core.handlers.wsgi
from tornado import web, ioloop, wsgi
from sockjs.tornado import SockJSRouter, SockJSConnection

# Set Django Environment
os.environ['DJANGO_SETTINGS_MODULE'] = 'games.settings'

from wsrequest import WebSocketRequest
from cah.views import SockAPIConnection

if __name__ == '__main__':
    port = int(os.environ.get("PORT", 8000))
    logging.getLogger().setLevel(logging.INFO)
    Router = SockJSRouter(SockAPIConnection, '/ws/api')

    wsgi_app = wsgi.WSGIContainer(django.core.handlers.wsgi.WSGIHandler())

    app = web.Application(
        Router.urls +
        [('.*', web.FallbackHandler, dict(fallback=wsgi_app)),]
    )
    app.listen(port)

    logging.info(' [*] Listening on 0.0.0.0:{}'.format(port))
    ioloop.IOLoop.instance().start()

